﻿/// <reference path="../core/jquery-1.4.2.min-vsdoc.js"/>
/// <reference path="../config/sw-config-default.js"/>
/// <reference path="../core/sw-prototype.js"/>
/// <reference path="../core/sw.js"/>
/// <reference path="../core/sw-control-core.js"/>

/***************************************************************************
jQuery Pager
NOTE: Created for SmartUI. 
Copyright 2010, Roy zhang
Features：
1.
2.
3.
4.
Update Note：

Needs：
****************************************************************************/

(function ($) {

    $.fn.pager = function (options, fnPageChanged) {
        /// <summary>pager</summary>
        /// <param name="options" type="Array">配置项</param>
        /// <param name="fnPageChanged" type="Function">fnPageChanged(pager,pageindex,pagesize)</param>
        ///#region 属性                
        options = $.extend({
            pagesize: 10,
            pageindex: 1,
            totalrecord: 0
        , pagesizes: iconfig.pager.pagesizes //可选择设定的每页结果数
        , issimple: false
//        , alwaysshow: false
        }, options);
        options.text = $.extend({
            pagesize: $iconst.controls.pager.pagesize
                , pageindex: $iconst.controls.pager.pageindex
                , frist: $iconst.controls.pager.frist
                , prve: $iconst.controls.pager.prve
                , next: $iconst.controls.pager.next
                , last: $iconst.controls.pager.last
                , reload: $iconst.controls.pager.reload
                , total: $iconst.controls.pager.total
        }, options.text);
        ///#endregion

        ///#region 主体内容
        return this.each(function () {
            var pager = $(this);
            var pagerInf = {}; //分页信息

            if (!pager.hasClass('pager')) {//判断是否已经生成pager
                pager.addClass("pager");
                pagerInf.pageNum = 1;
                pagerInf.pageSize = options.pagesize;
                var id = pager.attr("id");

                var pgroup = $(document.createElement('div')).addClass('pgroup');
                ///#region pagesize
                var sbpagesize = new StringBuilder();
                for (var i = 0; options.pagesizes[i]; i++) {
                    var size = String(options.pagesizes[i]);
                    sbpagesize.append('<option selected value="').append(size).append('">').append(size).append('</option>');
                }

                var selPageSize = $(document.createElement('select')).addClass('psize').append(sbpagesize.toString()).val(options.pagesize).attr('title', options.text.pagesize)
                                    .change(function () { pageChange(pager, 1, fnPageChanged); });

                pgroup.clone().append(selPageSize).appendTo(pager);

                ///#endregion
                if (options.issimple) {//简洁模式
                    pager.addClass("pager_simple");
                    selPageSize.hide();
                    //                    pgroup.append('<span class="pindex"></span><span>/</span><span class="ptotal">1</span>').appendTo(pager);
                    buildbtn(pager, 'prev').text(options.text.prve).appendTo(pager);
                    buildbtn(pager, 'next').text(options.text.next).appendTo(pager);
                }
                else {
                    ///#region Pager Change
                    pgroup.clone().append(buildbtn(pager, 'frist')).append(buildbtn(pager, 'prev')).appendTo(pager);

                    var txtIndex = $(document.createElement('input')).addClass('pindex').attr('title', options.text.pageindex)
                .keydown(function (e) { if (e.keyCode == 13) pageChange(pager, parseInt(this.value), fnPageChanged); });

                    pgroup.clone().append(txtIndex).append('<span>/</span><span class="ptotal">1</span>').appendTo(pager);

                    pgroup.clone().append(buildbtn(pager, 'next')).append(buildbtn(pager, 'last')).appendTo(pager);
                    ///#endregion
                    pgroup.clone().append(buildbtn(pager, 'reload')).appendTo(pager);
                    pgroup.append($(document.createElement('span')).text(options.text.total)).append($(document.createElement('span')).addClass('ptotalrecord')).appendTo(pager);
                }
            }
            else
                pagerInf = pager.data('inf');

            if (options.totalrecord)
                pagerInf.totalRecord = options.totalrecord;
            if (options.pageindex)
                pagerInf.index = options.pageindex;
            //        if (options.pagesize)
            //            pagerInf.pageSize = options.pagesize;

            setPager(pager, pagerInf, options.pageindex, options.pagesize);
        });
        ///#endregion

        ///#region 私有方法
        function buildbtn(pager, name) {
            /// <summary>生成翻页按钮</summary>
            var btn = $(document.createElement('a')).addClass('p' + name);
            switch (name) {
                case 'frist':
                    btn.attr('title', options.text.frist).click(function () { pageChange(pager, 1, fnPageChanged); });
                    break;
                case 'prev':
                    btn.attr('title', options.text.prve).click(function () { pageChange(pager, pager.data('inf').index - 1, fnPageChanged); });
                    break;
                case 'next':
                    btn.attr('title', options.text.next).click(function () { pageChange(pager, pager.data('inf').index + 1, fnPageChanged); });
                    break;
                case 'last':
                    btn.attr('title', options.text.last).click(function () { pageChange(pager, pager.data('inf').pageNum, fnPageChanged); });
                    break;
                case 'reload':
                    btn.attr('title', options.text.reload).click(function () { pageChange(pager, null, fnPageChanged); });
                    break;
            }
            return btn;
        }
        ///#endregion
    };
    $.fn.pagerChange = function (pageindex, fnPageChanged) {
        pageChange($(this), pageindex, fnPageChanged);
    }

    function setPager(pager, pagerInf, pageindex, pagesize) {
        /// <summary>设置Pager</summary>
        var ddlSize = pager.find('select.psize');
        if (pagesize)
            ddlSize.val(pagesize);

        pagesize = ddlSize.val() * 1;

        var pageNum = pagerInf.totalRecord > 0 ? parseInt(pagerInf.totalRecord / pagesize) + (pagerInf.totalRecord % pagesize > 0 ? 1 : 0) : 1;
        var isChange = false;
        //判断是否有变动
        if (pageindex) {
            if (pageindex > 0 && pageindex <= pageNum && (pagerInf.index != pageindex || pagesize != pagerInf.pageSize || pageNum != pagerInf.pageNum)) {
                pagerInf.pageSize = pagesize;
                pagerInf.index = pageindex;
                pagerInf.pageNum = pageNum;
                isChange = true;
            }
        }
        else//当为刷新操作时
            isChange = true;
        //设置显示的值
        pager.find('input.pindex').val(pagerInf.index);
        pager.find('span.ptotal').text(pageNum);
        pager.find('span.ptotalrecord').text(pagerInf.totalRecord);
        pager.data('inf', pagerInf);
        if (pagerInf.index < 2 && pagerInf.index == pageNum) {
            pager.hide();
        }
        else {
            pager.show();
            var btns = pager.find('a');
            btns.filter('.not').removeClass('not');
            if (pagerInf.index == pageNum)
                btns.filter('.pnext,.plast').addClass('not');
            else if (pagerInf.index < 2)
                btns.filter('.pfrist,.pprev').addClass('not');
        }
        return isChange;
    }

    function pageChange(pager, pageindex, fnPageChanged) {
        /// <summary>翻页事件</summary>
        //        if (!pager)
        //            pager = $('#' + id);
        if (pager.length > 0) {
            var pagerInf = pager.data('inf');
            if (pagerInf && setPager(pager, pagerInf, pageindex) && fnPageChanged)
                fnPageChanged(pagerInf.index, pagerInf.pageSize, pager);
        }
    }
})(jQuery);